package cn.hpclub.server.util;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;

import org.apache.commons.codec.digest.DigestUtils;

import com.jfinal.kit.StrKit;

import cn.hpclub.server.exception.CheckParamException;

public class DESAS{

    public static String strDefaultKey = "scwtxdw@123247@*&";
    private Cipher       encryptCipher = null;
    private Cipher       decryptCipher = null;

    public DESAS() throws Exception{
        this(strDefaultKey);
    }

    public DESAS(String strKey) throws Exception{
        int iID = Security.addProvider(new com.sun.crypto.provider.SunJCE());
        Key key = getKey(strKey.getBytes());
        encryptCipher = Cipher.getInstance("DES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
        decryptCipher = Cipher.getInstance("DES");
        decryptCipher.init(Cipher.DECRYPT_MODE, key);
    }

    public byte[] encrypt(byte[] arrB) throws Exception{
        return encryptCipher.doFinal(arrB);
    }

    public String encrypt(String strIn) throws Exception{
        return byteArr2HexStr(encrypt(strIn.getBytes()));
    }

    public byte[] decrypt(byte[] arrB) throws Exception{
        return decryptCipher.doFinal(arrB);
    }

    public String decrypt(String strIn) throws Exception{
        return new String(decrypt(hexStr2ByteArr(strIn)));
    }

    private Key getKey(byte[] arrBTmp) throws Exception{
        // ����һ���յ�8λ�ֽ����飨Ĭ��ֵΪ0��
        byte[] arrB = new byte[8];

        // ��ԭʼ�ֽ�����ת��Ϊ8λ
        for(int i = 0; i < arrBTmp.length && i < arrB.length; i++){
            arrB[i] = arrBTmp[i];
        }

        // ������Կ
        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
        return key;
    }

    public static byte[] hexStr2ByteArr(String strIn) throws Exception{
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        // �����ַ���ʾһ���ֽڣ������ֽ����鳤�����ַ������ȳ���2
        byte[] arrOut = new byte[iLen / 2];
        for(int i = 0; i < iLen; i = i + 2){
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte)Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

    public static String byteArr2HexStr(byte[] arrB) throws Exception{
        int iLen = arrB.length;

        StringBuffer sb = new StringBuffer(iLen * 2);
        for(int i = 0; i < iLen; i++){
            int intTmp = arrB[i];
            // �Ѹ���ת��Ϊ����
            while(intTmp < 0){
                intTmp = intTmp + 256;
            }
            // С��0F������Ҫ��ǰ�油0
            if(intTmp < 16){
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception{

        DESAS des = new DESAS();
        String encryStr = des.encrypt("6#62#37#20141106151201");
        System.out.println("xx?fxstr=" + encryStr);

        System.out.println(new String(des.decrypt(hexStr2ByteArr(encryStr))));
    }

    /**
     * @Title: generateKey
     * @Description:根据token，生成DESC动态Key
     * @param src
     * @return String
     * @throws CheckParamException
     */
    public static String generateKey(String src) throws CheckParamException{
        if(StrKit.isBlank(src)){
            throw new CheckParamException("生成key异常");
        }
        String md5 = DigestUtils.md5Hex(src);
        if((md5 != null) && (md5.length() >= 8)){
            // System.out.println("md5:" + md5 + ",key:" + key);
            String key = md5.substring(md5.length() - 8);
            return key;
        } else{
            throw new CheckParamException("生成key异常");
        }
    }
}
